Create an R function that defines a global variable called
x with a value of 5. Inside the function, declare a local
variable also named x with a value of 10. Print the value
of x both inside and outside the function to demonstrate
shadowing.
Solutions:
# Enter code here
x <- 5
shadowing <- function() {
x <- 10
print(paste("Inside the function, x is:", x))
}
shadowing()
## [1] "Inside the function, x is: 10"
print(paste("Outside the function, x is:", x))
## [1] "Outside the function, x is: 5"
Create an R function that takes an argument and adds it to a global
variable called total. Call the function multiple times
with different arguments to accumulate the values in
total.
Solutions:
# Enter code here
#this is to initiate the variable
total <- 0
add_total <- function(number) {
total <<- total + number
print(paste("total is now ", total))
}
add_total(5)
## [1] "total is now 5"
add_total(5)
## [1] "total is now 10"
add_total(5)
## [1] "total is now 15"
Write an R program that includes a global variable total
with an initial value of 100. Create a function that takes an argument,
adds it to total, and returns the updated
total. Demonstrate how this function interacts with the
global variable.
Solutions:
# Enter code here
total <- 100
add_total <- function(number) {
total <<- total + number
return(total)
}
new_total <- add_total(10)
sprintf ("The new total is %d", new_total)
## [1] "The new total is 110"
new_total <- add_total(20)
sprintf ("The new total is %d", new_total)
## [1] "The new total is 130"
new_total <- add_total(30)
sprintf ("The new total is %d", new_total)
## [1] "The new total is 160"
Define a function outer_function that declares a local
variable x with a value of 5. Inside
outer_function, define another function
inner_function that prints the value of x.
Call both functions to show how the inner function accesses the variable
from the outer function’s scope.
Solutions:
# Enter code here
outer_function <- function(){
x <- 5
inner_function <- function() {
sprintf ("The value of x is %d", x)
}
inner_function()
}
outer_function()
## [1] "The value of x is 5"
Create a function that takes a text input and generates a humorous
meme with the text overlaid on an image of your choice. You can use the
magick package for image manipulation. You can find more
details about the commands offered by the package, with some examples of
annotating images here: https://cran.r-project.org/web/packages/magick/vignettes/intro.html
Solutions:
# Enter code here
library(magick)
## Linking to ImageMagick 6.9.12.93
## Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fftw, ghostscript, x11
create_meme <- function(image_path, meme_text) {
img <- image_read(image_path)
# add text
meme <- img %>%
image_annotate(text = meme_text,
gravity = 'south',
location = "+0+100",
size = 200,
font = "Impact",
strokecolor = "black",
color = "white")
return(meme)
}
create_meme("/Users/marzuki/Desktop/NM2207/NM2207/Week-5/meme.png", "Me after NM2207")